import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart' hide Action;
import 'package:www.znpigai.teacher/global_store/action.dart';
import 'package:www.znpigai.teacher/global_store/state.dart';
import 'package:www.znpigai.teacher/global_store/store.dart';
import 'package:www.znpigai.teacher/utils/http.dart';
import 'package:www.znpigai.teacher/utils/nav.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Effect<MineState> buildEffect() {
  return combineEffects(<Object, Effect<MineState>>{
    MineAction.onLogout: _onLogout,
    MineAction.onClass: _onClass,
    MineAction.onProfile: _onProfile,
    MineAction.onAbout: _onAbout,
    MineAction.onPay: _onPay,
    MineAction.onMessage: _onMessage,
    MineAction.onFeedback: _onFeedback,
    MineAction.onAvatar: _onAvatar,
  });
}

void _onAvatar(Action action, Context<MineState> ctx) async {
  if (ctx.state.userInfo == null || ctx.state.userInfo.isEmpty) {
    Util.alert('没有用户信息', ctx.context);
    return;
  }

  var url = await Navigator.of(ctx.context).pushNamed(Nav.AVATAR_LIST);
  if (url != null) {
    var userInfo = ctx.state.userInfo;
    userInfo['headImage'] = url;
    GlobalStore.store.dispatch(GlobalActionCreator.onChangeUserInfo(userInfo));
    ctx.dispatch(MineActionCreator.refresh());
  }
}

void _onLogout(Action action, Context<MineState> ctx) {
  Util.showConfirmDialog('退出登录', '确定要退出登录？', ctx.context).then((ok) {
    if (ok == true) {
      GlobalState.isSignIn = false;
      Navigator.of(ctx.context).pushNamedAndRemoveUntil(
          GlobalState.isTeacher ? Nav.SOCIAL_LOGIN : Nav.LOGIN,
          (Route<dynamic> route) => false);
    }
  });
}

void _onClass(Action action, Context<MineState> ctx) {
  if (ctx.state.userInfo == null || ctx.state.userInfo.isEmpty) {
    Util.alert('没有用户信息', ctx.context);
    return;
  }

  Navigator.of(ctx.context).pushNamed(Nav.CLASS_LIST);
}

void _onProfile(Action action, Context<MineState> ctx) {
  if (ctx.state.userInfo == null || ctx.state.userInfo.isEmpty) {
    Util.alert('没有用户信息', ctx.context);
    return;
  }

  Navigator.of(ctx.context).pushNamed(Nav.PROFILE);
}

void _onAbout(Action action, Context<MineState> ctx) {
  Navigator.of(ctx.context).pushNamed(Nav.ABOUT);
}

void _onPayList(Action action, Context<MineState> ctx) {
  if (ctx.state.userInfo == null || ctx.state.userInfo.isEmpty) {
    Util.alert('没有用户信息', ctx.context);
    return;
  }

  Navigator.of(ctx.context).pushNamed(Nav.PAY_LIST);
}

void _onPay(Action action, Context<MineState> ctx) async {
  if (ctx.state.userInfo == null || ctx.state.userInfo.isEmpty) {
    Util.alert('没有用户信息', ctx.context);
    return;
  }

  Navigator.of(ctx.context).pushNamed(Nav.PAY).then((ret) async {
    if (ret != null) {
      // 需要刷新用户信息
      var url = "/correct/${GlobalState.userRole}/api/profile.mvc";
      var result = await Http.get(url);

      if (result.success) {
        GlobalStore.store
            .dispatch(GlobalActionCreator.onChangeUserInfo(result.data));
        ctx.dispatch(MineActionCreator.refresh());
      } else {
        Util.alertFail(result, ctx.context);
      }
    }
  });
}

void _onMessage(Action action, Context<MineState> ctx) {
  if (ctx.state.userInfo == null || ctx.state.userInfo.isEmpty) {
    Util.alert('没有用户信息', ctx.context);
    return;
  }

  Navigator.of(ctx.context).pushNamed(Nav.MESSAGE_LIST);
}

void _onFeedback(Action action, Context<MineState> ctx) {
  if (ctx.state.userInfo == null || ctx.state.userInfo.isEmpty) {
    Util.alert('没有用户信息', ctx.context);
    return;
  }

  Navigator.of(ctx.context).pushNamed(Nav.WEB_PAGE, arguments: {
    'url': '/correct/pub/view/feedback/index.mvc',
    'share': false,
    'done': ''
  });
}
